﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class SelectWindow : EditorWindow
{
    static SelectWindow window;
    Vector2 scrollPos;
    string[] _fileNames = null;
    bool[] _selectInfos = null;
    Action _action;

    public static List<string> FileNames
    {
        get
        {
            List<string> fileNames = new List<string>();


            for (int i = 0, iMax = window._fileNames.Length; i < iMax; ++i)
            {
                if (window._selectInfos[i])
                {
                    fileNames.Add(window._fileNames[i]);
                }
            }

            return fileNames;
        }
    }

    public static void Open(string[] fileNames, Action action = null)
    {
        window = EditorWindow.GetWindow(typeof(SelectWindow)) as SelectWindow;

        window._fileNames = fileNames;
        window._selectInfos = new bool[fileNames.Length];
        window._action = action;
    }


    void SetSelectInfo(bool isSelect)
    {
        for (int i = 0, iMax = _selectInfos.Length; i < iMax; ++i)
        {
            _selectInfos[i] = isSelect;
        }
    }


    void OnGUI()
    {
        if (null == _fileNames || _fileNames.Length <= 0
            || null == _selectInfos || _selectInfos.Length <= 0)
            return;

        // 标题
        EditorGUILayout.BeginVertical("Box");
        GUIStyle style = new GUIStyle();
        style.fontSize = 15;
        style.normal.textColor = Color.white;
        style.alignment = TextAnchor.MiddleCenter;
        GUILayout.Label("选择你要创建脚本信息的文件", style);
        EditorGUILayout.EndVertical();

        // 内容
        scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
        {
            for (int i = 0, iMax = _fileNames.Length; i < iMax; ++i)
            {
                string fileName = _fileNames[i];

                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField(fileName);
                    _selectInfos[i] = EditorGUILayout.Toggle(_selectInfos[i], GUILayout.Width(20));
                }
                EditorGUILayout.EndHorizontal();
            }
        }
        EditorGUILayout.EndScrollView();

        // 全选和全不选按钮
        EditorGUILayout.BeginHorizontal();
        {
            if (GUILayout.Button("All"))
            {
                SetSelectInfo(true);
            }

            if (GUILayout.Button("None"))
            {
                SetSelectInfo(false);
            }
        }
        EditorGUILayout.EndHorizontal();

        // 确定按钮
        if (GUILayout.Button("确定生成"))
        {
            try
            {
                if (null == FileNames || FileNames.Count <= 0)
                {
                    Debug.Log("无文件选中");
                    return;
                }

                if (null != _action)
                {
                    _action();
                    window.Close();
                }
            }
            catch(Exception e)
            {
                Debug.LogError("[Error]SelectWindow::OnGUI. " + e.StackTrace);
            }
        }
    }
}